package entity.task;

import entity.tasktype.*;
import entity.user.*;
import entity.resource.*;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "TASK")
@Inheritance(strategy = InheritanceType.JOINED)
public class Task implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private Long id;
	
	@NotNull
	@Size(min = 3, max = 100)
	@Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces")
	private String name;
	
	@NotNull
	@Size(min = 3, max = 1000)
	@Pattern(regexp = "[A-Za-z. ]*", message = "must contain only letters, spaces and dot")
	private String description;
	
	@NotNull
	private Float estimated;
	
	@NotNull
	private Float remaining;
	
	@NotNull
	private Float completed;
	
	@NotNull
	@Size(min = 2, max = 3)
	private String fulfill;

	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name = "tasktype_id")
	private TaskType taskType;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id")
	private User user;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "resource_id")
	private Resource resource;

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Task(Long id, String name, String description, Float estimated,
			Float remaining, Float completed,
			String fulfill, TaskType taskType, User user, Resource resource) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.estimated = estimated;
		this.remaining = remaining;
		this.completed = completed;
		this.fulfill = fulfill;
		this.taskType = taskType;
		this.user = user;
		this.resource = resource;
	}

	public TaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}

	public Long getId() {
		return id;
	}

	public String getFulfill() {
		return fulfill;
	}

	public void setFulfill(String fulfill) {
		this.fulfill = fulfill;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Float getEstimated() {
		return estimated;
	}

	public void setEstimated(Float estimated) {
		this.estimated = estimated;
	}

	public Float getRemaining() {
		return remaining;
	}

	public void setRemaining(Float remaining) {
		this.remaining = remaining;
	}

	public void setCompleted(Float completed) {
		this.completed = completed;
	}

	public Float getCompleted() {
		return completed;
	}

	public Task() {
		super();
		taskType = new TaskType();
	}

	@Override
	public String toString() {
		return "Task [id=" + id + ", name=" + name + ", description="
				+ description + ", estimated=" + estimated + ", fulfill=" + fulfill
				+ ", taskType=" + taskType + ", user=" + user + " resource=" + resource + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((fulfill == null) ? 0 : fulfill.hashCode());
		result = prime * result + ((estimated == null) ? 0 : estimated.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((taskType == null) ? 0 : taskType.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		result = prime * result + ((resource == null) ? 0 : resource.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (fulfill == null) {
			if (other.fulfill != null)
				return false;
		} else if (!fulfill.equals(other.fulfill))
			return false;
		if (estimated == null) {
			if (other.estimated != null)
				return false;
		} else if (!estimated.equals(other.estimated))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (taskType == null) {
			if (other.taskType != null)
				return false;
		} else if (!taskType.equals(other.taskType))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		if (resource == null) {
			if (other.resource != null)
				return false;
		} else if (!resource.equals(other.resource))
			return false;
		return true;
	}
		
}